package GUI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;

import service.Service;
public class RemoteControlGUI extends Thread implements GUI {


	Display display;	
	Shell gui;
	
	static Object someObject=new Object();
	Button stop,start;
	
	public RemoteControlGUI(){
		
	}
	
	public void showGUI(){
		
		
		display.asyncExec(new Runnable(){

			@Override
			public void run() {
				
					
					
					//opening the visible shell
					gui.setVisible(true);
					gui.open();
				
			}});
		
	}
	
	public synchronized void hide() throws Exception
	{
		display.asyncExec(new Runnable(){

			@Override
			public void run() {
				
					gui.setVisible(false);
				
			}});


	}
	
	
	public void run()
	{
		synchronized(someObject)
		{
			if(display==null)
			{
				display=new Display();
			}
		
			
			gui=new Shell(display,SWT.MIN);
			gui.setText("Remote Control");
			
			//a grid layout with 2 columns
			GridLayout gLayout=new GridLayout(2,false);
			
			gui.setLayout(gLayout);
			GridData gData=new GridData();

			//creating a start button
			start=new Button(gui,SWT.READ_ONLY);
			start.setText("start");


			
			//creating a stop button
			stop=new Button(gui,SWT.READ_ONLY);
			stop.setText("stop");
			
			gData=new GridData();
			gData.grabExcessHorizontalSpace=true;
			gData.grabExcessVerticalSpace=true;
			gData.horizontalAlignment=SWT.FILL;
			gData.verticalAlignment=SWT.FILL;
			gData.horizontalSpan=2;
			
			stop.setEnabled(false);
			
				
			//adding listener to start button which opens file dialog
			start.addSelectionListener(new SelectionListener(){

				@Override
				public void widgetSelected(SelectionEvent e) {
					
					Service.getInstance().start();
					start.setEnabled(false);
				
					stop.setEnabled(true);
					
		
				}

				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
					// TODO Auto-generated method stub
					
				}

			});
			
			
			//adding listener to stop button which stops simulation
			stop.addSelectionListener(new SelectionListener(){

				@Override
				public void widgetSelected(SelectionEvent e) {
					
			
				Service.getInstance().stop();
				
		
				stop.setEnabled(false);
				start.setEnabled(true);
				}

				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
					// TODO Auto-generated method stub
					
				}});
			
	
			
			

			
gui.pack();
			gui.setVisible(true);

			
		}
		
		//read and dispatch loop
		while (!gui.isDisposed()) {
		      if (!display.readAndDispatch()) {
		        display.sleep();
		      }
		      
	}  
		
	
	}


	public static void main(String[]args)
	{
		
		new RemoteControlGUI().start();
	}
	
	
}
